package cn.ad.service;

import cn.ad.entity.BaseReportValue;
import cn.ad.handler.SqlSessionHandler;
import cn.ad.model.BaseRptModel;
import cn.ad.model.IBaseRptModel;
import cn.ad.model.RptDetailModel;
import cn.ad.model.Search;
import cn.ad.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CampaignRptService {

	@Autowired
	private SqlSessionHandler sqlSessionHandler;

	public IBaseRptModel getCampaignSumRpt(List<Integer> cpnIds) {
		
		BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptSumCampaignMapper.selectSumByList", cpnIds);
		BaseRptModel result = new BaseRptModel();
		if (baseRptValue != null) {
			result.setClick(baseRptValue.getClick());
			result.setPv(baseRptValue.getPv());
			result.setCost(baseRptValue.getCost());
		}
		return result;
	}

	public IBaseRptModel getCampaignRpt(int cpnId) {
		List<Integer> list = new ArrayList<>();
		list.add(cpnId);
		BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptSumCampaignMapper.selectSumByList", list);
		BaseRptModel result = new BaseRptModel();
		if (baseRptValue != null) {
			result.setClick(baseRptValue.getClick());
			result.setPv(baseRptValue.getPv());
			result.setCost(baseRptValue.getCost());
		}
		return result;
	}

	public List<IBaseRptModel> getCampaignRpts(List<Integer> cpnIds) {
		List<IBaseRptModel> list = new ArrayList<>();
		for (Integer cpnId : cpnIds) {
			BaseRptModel result = new BaseRptModel();
			result.setId(cpnId);
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptSumCampaignMapper.selectOneById", cpnId);
			if (baseRptValue != null) {
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			list.add(result);
		}
		return list;
	}

	public Map<Integer, IBaseRptModel> getCampaignSumRpts(Map<Integer, List<Integer>> ads) {
		Map<Integer, IBaseRptModel> resultMap = new HashMap<Integer, IBaseRptModel>();
		for (Integer key : ads.keySet()) {
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptSumCampaignMapper.selectSumByList", ads.get(key));
			BaseRptModel result = new BaseRptModel();
			if (baseRptValue != null) {
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			resultMap.put(key, result);
		}
		return resultMap;
	}

	/**
	 * 查询时间段内的单个订单的报告，算总计
	 */
	public IBaseRptModel getCampaignRpt(int cpnId, Date startTime, Date endTime) {
		//Utils.isCurrentTime(startTime, endTime);
		
		Search condition = new Search(cpnId,startTime,endTime);
		BaseRptModel result = new BaseRptModel();
		
		Date currentDateStartTime = Utils.getStartTime(Calendar.getInstance().getTime());
		if(currentDateStartTime.getTime()<startTime.getTime()){
			return new BaseRptModel();
		}
		//当开始时间是今天
		if(currentDateStartTime.getTime()==startTime.getTime()){
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCampaignMapper.hourly:sum", condition);
			if(baseRptValue!=null){
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			return result;
		}
		
		//当开始时间是今天之前。结束时间是今天之前
		if(endTime.before(currentDateStartTime)){
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCampaignMapper.daily:sum", condition);
			if(baseRptValue!=null){				
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			return result;
		}
		//当开始时间是今天之前。结束时间是今天或今天之后
		if(currentDateStartTime.equals(endTime)||currentDateStartTime.before(endTime)){
			Search where = new Search(cpnId, startTime, Utils.nextDay(currentDateStartTime,-1));
			//算整天的
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCampaignMapper.daily:sum", where);
			if(baseRptValue!=null){
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			//算今天的
			where.setStartTime(currentDateStartTime);
			baseRptValue = sqlSessionHandler.selectOne("rptCampaignMapper.hourly:sum", where);
			if(baseRptValue!=null){				
				result.setClick(result.getClick()+baseRptValue.getClick());
				result.setPv(result.getPv()+baseRptValue.getPv());
				result.setCost(result.getCost()+baseRptValue.getCost());
			}
			return result;
		}
		return result;
	}
	/**
	 * 查询某一天的时报列表，以hour分组
	 */
	public List<RptDetailModel> getCampaignRptHour(int campaignId, Date day) {
		Search model = new Search();
		model.setStartTime(day);
		model.setCampaignId(campaignId);
		List<RptDetailModel> list = sqlSessionHandler.selectList("rptCampaignMapper.hourly:list", model);
		return list;
	}

	public List<RptDetailModel> getCampaignRptHour(int campaignId, Date startTime, Date endTime) {
		Search model = new Search();
		model.setStartTime(startTime);
		model.setEndTime(endTime);
		model.setCampaignId(campaignId);
		SimpleDateFormat format=new SimpleDateFormat("HH");
		String startHour = format.format(startTime);
		String endHour = format.format(endTime);
		model.setStartHour(Integer.parseInt(startHour));
		model.setEndHour(Integer.parseInt(endHour));
		List<RptDetailModel> list = sqlSessionHandler.selectList("rptCampaignMapper.hourly:list:in:time", model);
		return list;
	}

	/**
	 * 查询时间段内的单个订单的报告，以天分组
	 */
	public List<RptDetailModel> getCampaignRptDay(int campaignId, Date startTime, Date endTime) {
		Utils.isCurrentTime(startTime, endTime);
		Search search = new Search(campaignId, startTime, endTime);
		List<RptDetailModel> result = new ArrayList<>();
		
		Date currentDateStartTime = Utils.getStartTime(Calendar.getInstance().getTime());
		
		//选择的今天之后的数据
		if(startTime.after(currentDateStartTime)){
			return result;
		}
		
		//当开始时间是今天
		if(currentDateStartTime.equals(startTime)){
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCampaignMapper.hourly:sum", search);
			RptDetailModel model= new RptDetailModel();
			model.setDescription(Utils.dateToString(currentDateStartTime, Utils.LONG_DATE_FORMAT));
			if(baseRptValue!=null){
				model.setClick(baseRptValue.getClick());
				model.setPv(baseRptValue.getPv());
				model.setCost(baseRptValue.getCost());
			}
			if(model.getPv()!=0||model.getClick()!=0||model.getCost()!=0){
				result.add(model);
			}
			return result;
		}
		
		//结束时间是今天之前
		if(endTime.before(currentDateStartTime)){
			result = sqlSessionHandler.selectList("rptCampaignMapper.daily:list", search);
			return result;
		}
		//当开始时间是今天之前。结束时间是今天或今天之后
		if(currentDateStartTime.equals(endTime)||currentDateStartTime.before(endTime)){
			Search where = new Search(campaignId, startTime, Utils.nextDay(currentDateStartTime,-1));
			//算整天的
			result = sqlSessionHandler.selectList("rptCampaignMapper.daily:list", where);
			
			//算今天的
			where.setStartTime(currentDateStartTime);
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCampaignMapper.hourly:sum", where);
			RptDetailModel model= new RptDetailModel();
			model.setDescription(Utils.dateToString(currentDateStartTime, Utils.LONG_DATE_FORMAT));
			if(baseRptValue!=null){
				model.setClick(baseRptValue.getClick());
				model.setPv(baseRptValue.getPv());
				model.setCost(baseRptValue.getCost());
			}
			if(result==null){
				result = new ArrayList<>();
			}
			if(model.getPv()!=0||model.getClick()!=0||model.getCost()!=0){
				result.add(model);
			}
			return result;
		}
		return result;
		
	}

	public List<IBaseRptModel> getCampaignOsRpts(int campaignId, Date startTime, Date endTime) {
		Search where = new Search(campaignId, startTime, endTime);
		List<IBaseRptModel> list = sqlSessionHandler.selectList("rptCampaignMapper.os:sum:list", where);
		return list;
	}

	public List<IBaseRptModel> getCampaignConnectTypeRpts(int campaignId, Date startTime, Date endTime) {
		Search where = new Search(campaignId, startTime, endTime);
		List<IBaseRptModel> list = sqlSessionHandler.selectList("rptCampaignMapper.connectType:sum:list", where);
		return list;
	}

	public List<IBaseRptModel> getCampaignOperatorRpts(int campaignId, Date startTime, Date endTime) {
		Search where = new Search(campaignId, startTime, endTime);
		List<IBaseRptModel> list = sqlSessionHandler.selectList("rptCampaignMapper.operator:sum:list", where);
		return list;
	}

	/**
	 * 查询订单的资源平台纬度的数据报告
	 */
	public List<IBaseRptModel> getCampaignAdxTypeRpts(int campaignId, Date startTime, Date endTime) {
		if (startTime == null || endTime == null) {
			throw new IllegalArgumentException("开始时间和结束时间不能为null。数据错误。");
		}
		
        Date now = Utils.getStartTime(Calendar.getInstance().getTime());
        
        // 查询时间为将来的某一段时间 返回空
		if (startTime.getTime() > now.getTime()) {
			return new ArrayList<>();
		}
		
		Map<String, Object> searchParam = new HashMap<>();
		searchParam.put("campaignId", campaignId);
		
		// 今天 查询时报
		if (startTime.getTime() == endTime.getTime() && startTime.getTime() == now.getTime()) {
			searchParam.put("isToday", 0);
		}
		
		// 过去的某一天 查询日报
		if (startTime.getTime() == endTime.getTime() && endTime.getTime() < now.getTime()) {
			searchParam.put("isToday", 1);
			searchParam.put("startTime", startTime);
			searchParam.put("endTime", endTime);
		}
		
		// 一段时间的报告
		if (startTime.getTime() < now.getTime() && endTime.getTime() >= now.getTime()) {
			// 过去的日报+今天的时报
			searchParam.put("isToday", 2);
			searchParam.put("startTime", startTime);
			searchParam.put("endTime", endTime);
		} else if (endTime.getTime() < now.getTime()) {
			// 日报
			searchParam.put("isToday", 1);
			searchParam.put("startTime", startTime);
			searchParam.put("endTime", endTime);
		}
		return sqlSessionHandler.selectList("rptCampaignAdxTypeMapper.rpt:campaign:adxtype:sum", searchParam);
	}

	/**
	 * 根据订单ID列表与指定时间段计算数据总计
	 * @param ids 订单ID列表
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return
	 */
	public BaseReportValue getCampaignSumByCids(List<Integer> ids, Date startTime, Date endTime) {
		Date currentDateStartTime = Utils.getStartTime(Calendar.getInstance().getTime());
		if(currentDateStartTime.getTime()<startTime.getTime()){
			return new BaseReportValue();
		}
		Search search=new Search();
		search.setIds(ids);
		search.setStartTime(startTime);
		search.setEndTime(endTime);
		Utils.resizeSearch(search,startTime,endTime);
		return sqlSessionHandler.selectOne("rptCampaignMapper.campaign:rpt:sum",search);
	}
}
